home *** CD-ROM | disk | FTP | other *** search
- Path: news.ucdavis.edu!usenet
- From: Mike Ransom <mlransom@ucdavis.edu>
- Newsgroups: comp.lang.c++
- Subject: Streams problem--simple? (help debug)
- Date: Tue, 26 Mar 1996 17:41:16 -0800
- Organization: UC Davis, Dept. of Agronomy & Range Science
- Message-ID: <31589CBC.7AD9@ucdavis.edu>
- NNTP-Posting-Host: zippy.ucdavis.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Sorry to start out my 1st post with something so big. Hopefully by
- helping me, you will help others as well. TIA
-
- Could someone please comment on (and degbug :) ) the following program?
- I'm just starting with C++ and am trying to be a good boy and learn the
- streams way of doing things. I've pared out some non-essential code for
- this posting.
-
- The program is fairly simple. It reads a tabular ASCII file of numbers
- and outputs same, after some calculations. In this example, I'm reading
- lines into a string stream buffer, which I know isn't necessary for this
- sample, but I need to work with such a buffer for other purposes in
- other processing.
-
- The problem behavior is that the the program will run correctly the
- first time after it is compiled, but then apparently fail to read the
- input file correctly in subsequent runs. The subsequent executions will
- output the correct number of lines (showing a counter value) for the
- input file, but not the values I've attempted to calculate/output. This
- is a little exasperating, since the program exits normally (I think) and
- I've explicitly closed the files--and everything should go out of scope
- anyway right??? This is running in a DOS window under Windows. I'm
- using the Turbo C++ 3.0 for DOS compiler (that isn't the problem is
- it???). I've tried it in DOS too, under DOS 5.0 and 6.22. Sometimes it
- will crash the computer.
-
- I suspect that my problem is related to EOL or EOF problems or get and
- put pointer locations. I wasn't sure what the conventional way of
- extracting the EOL character from the input stream is; I took the
- approach of using the "getline" function. As an aside you can see from
- comments, I was having problems with setting the default output width
- and with setting numerical precision of 0 decimal places.
-
- /**********************************************************
- SAMPLES OF INPUT, OUTPUT:
-
-
- INPUT FILE:
-
- 1 1 6.40 40.40 14.68 3.45 3.96 9.06 1.42
- 1 2 7.45 72.57 13.44 2.55 4.54 8.97 1.59
- 1 3 5.30 27.58 18.65 5.45 7.45 9.54 1.68
- 1 4 2.75 14.07 26.04 10.42 11.42 10.75 1.97
- 1 5 3.65 27.82 30.34 13.18 12.90 11.60 2.46
- 1 6 2.15 2.25 34.44 15.48 14.65 12.02 2.51
- 1 7 .20 .22 36.45 16.73 14.85 12.93 2.23
- 1 8 1.15 2.80 35.15 15.85 12.92 12.48 2.09
- 1 9 3.70 12.61 30.89 13.61 9.52 11.96 1.79
- 1 10 2.35 7.86 25.15 9.69 7.16 11.08 1.37
- 1 11 2.65 9.83 20.05 6.55 5.20 10.10 1.30
- 1 12 6.60 30.29 16.02 4.10 3.73 9.39 1.72
-
-
- SCREEN OUTPUT OF 1ST RUN
-
- [WIN] 14:09:47.66 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
- Using input file x.x
- Writing output file x
- 1* 1 1 40.4 4.0 9.1 0.25 5.07 1.43 1.43 11.4 -29.0
- 2* 1 2 72.6 4.5 8.0 0.18 12.83 1.63 1.63 9.2 -63.3
- 3* 1 3 27.6 7.4 12.0 0.45 1.89 2.68 1.89 39.2 11.6
- 4* 1 4 14.1 11.4 18.2 1.02 0.43 4.11 0.43 135.1 121.1
- 5* 1 5 27.8 12.9 21.8 1.45 0.60 4.64 0.60 216.5 188.7
- 6* 1 6 2.3 14.6 25.0 1.96 0.04 5.27 0.04 333.2 330.9
- 7* 1 7 0.2 14.9 26.6 2.19 0.00 5.35 0.00 377.2 377.0
- 8* 1 8 2.8 12.9 25.5 2.02 0.04 4.65 0.04 302.4 299.6
- 9* 1 9 12.6 9.5 22.3 1.49 0.26 3.43 0.26 164.6 151.9
- 10* 1 10 7.9 7.2 17.4 0.88 0.28 2.58 0.28 73.3 65.5
- 11* 1 11 9.8 5.2 13.3 0.52 0.59 1.87 0.59 31.2 21.4
- 12* 1 12 30.3 3.7 10.1 0.29 3.19 1.34 1.34 12.7 -17.6
-
- [WIN] 14:10:11.33 G:\MIKE\SAMSUL\ANALYSIS >
-
-
- SCREEN OUTPUT OF SUBSEQUENT RUNS
-
- [WIN] 14:15:06.72 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
- Using input file x.x
- Writing output file x
- 1*
- 2*
- 3*
- 4*
- 5*
- 6*
- 7*
- 8*
- 9*
- 10*
- 11*
- 12*
-
- [WIN] 14:15:10.13 G:\MIKE\SAMSUL\ANALYSIS >
-
- */
-
- ///Look in areas near the triple slash for notes regarding the problem
-
- #include <iostream.h>
- #include <fstream.h>
- #include <iomanip.h>
- #include <strstream.h>
- #include <math.h>
-
- /******************************************************************/
- // MAIN
- main(int argc, char *argv[]) {
- register int i=0; //generic counter
-
- // OPEN INPUT, OUTPUT FILES
- ifstream InpF (argv[1], ios::nocreate);
- if (!InpF) {
- cout << "Problem opening input file " <<argv[1] <<"\n\n";
- return 1;
- }
- ofstream OutF("TEST.OUT");
- if (!OutF) {
- cout << "Problem opening output file!\n\n";
- return 1;
- }
-
- /******************************************************************/
- //READ LINES FROM WEATHER FILE
-
- //Inputs
- int Loc, Mo;
- float WetDays, TotRain, TMax, TMin, Solar, VP, Wind;
-
- //Intermediates
- float TAvg, VPs, VPd;
- char *SBuf, *Output;
- long getpointer;
- strstream SSBuf(SBuf,100,(ios::in | ios::out));
-
- //Outputs:
- float Ph20, PSol, PLim;
- float H2Oneed, H2Odef;
-
- SSBuf.setf (
- ios::right |
- ios::fixed |
- ios::showpoint
- );
- SSBuf.width(8); //How do you set default width???
- SSBuf.precision(1); //(don't use (0)--defaults to 6 places!)
- //How to set precision to 0???
- //SSBuf.unsetf(ios::skipws);
-
- //Loop thru input file:
- while (InpF >>Loc >>Mo >>WetDays >>TotRain >>TMax >>TMin >>Solar >>VP >>Wind
- && OutF) {
-
- ///Have been experimenting with both of these
- ///Seems like they both should do same thing
- InpF.getline(SBuf,10,'\n');
- //InpF.ignore(10,'\n');
-
- //Calculate new variable values:
- TAvg = .5 * (TMax + TMin);
- VPs = 6.108 * exp((17.27 * TAvg)/(237.3+TAvg));
- VPd = (VPs - VP) * .1; //mbar to kPa
- Ph20 = (TotRain/VPd)* 3.1 * 10 *.001; //in t/ha
- PSol = (Solar * 30) * 1.2 * 10 *.001;
- if (Ph20 < PSol) PLim = Ph20; else PLim = PSol; //Limiting factor
- H2Oneed = (PSol * VPd) / (3.1 * 10 * .001);
- H2Odef = H2Oneed - TotRain;
-
- //store in string stream buffer:
- SSBuf.seekp(0);
- SSBuf
- << setw(4) << Loc
- << setw(3) << Mo
- << setprecision(1)
- << setw(6) << TotRain
- << setw(6) << Solar
- << setw(6) << TAvg
- << setprecision(2)
- << setw(6) << VPd
- << setw(8) << Ph20
- << setw(8) << PSol
- << setw(8) << PLim
- << setprecision(1)
- << setw(8) << H2Oneed
- << setw(8) << H2Odef
- << endl;
-
- SSBuf.seekg(0); //reposition get pointer
- SSBuf.getline(Output,100,'\n');
-
- cout <<setw(5) << ++i <<"*" <<Output << endl; //output to screen
- ///Note: on running program 2nd time, only i and "*" get output
- OutF << Output << endl; //Output to file
- }
-
- /**********************************************************/
- //CLOSE FILES
- InpF.close();
- OutF.close();
-
- /**********************************************************/
- return 0;
- }
-
- /**********************************************************
- SAMPLES OF INPUT, OUTPUT:
-
-
- INPUT FILE:
-
- 1 1 6.40 40.40 14.68 3.45 3.96 9.06 1.42
- 1 2 7.45 72.57 13.44 2.55 4.54 8.97 1.59
- 1 3 5.30 27.58 18.65 5.45 7.45 9.54 1.68
- 1 4 2.75 14.07 26.04 10.42 11.42 10.75 1.97
- 1 5 3.65 27.82 30.34 13.18 12.90 11.60 2.46
- 1 6 2.15 2.25 34.44 15.48 14.65 12.02 2.51
- 1 7 .20 .22 36.45 16.73 14.85 12.93 2.23
- 1 8 1.15 2.80 35.15 15.85 12.92 12.48 2.09
- 1 9 3.70 12.61 30.89 13.61 9.52 11.96 1.79
- 1 10 2.35 7.86 25.15 9.69 7.16 11.08 1.37
- 1 11 2.65 9.83 20.05 6.55 5.20 10.10 1.30
- 1 12 6.60 30.29 16.02 4.10 3.73 9.39 1.72
-
-
- SCREEN OUTPUT OF 1ST RUN
-
- [WIN] 14:09:47.66 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
- Using input file x.x
- Writing output file x
- 1* 1 1 40.4 4.0 9.1 0.25 5.07 1.43 1.43 11.4 -29.0
- 2* 1 2 72.6 4.5 8.0 0.18 12.83 1.63 1.63 9.2 -63.3
- 3* 1 3 27.6 7.4 12.0 0.45 1.89 2.68 1.89 39.2 11.6
- 4* 1 4 14.1 11.4 18.2 1.02 0.43 4.11 0.43 135.1 121.1
- 5* 1 5 27.8 12.9 21.8 1.45 0.60 4.64 0.60 216.5 188.7
- 6* 1 6 2.3 14.6 25.0 1.96 0.04 5.27 0.04 333.2 330.9
- 7* 1 7 0.2 14.9 26.6 2.19 0.00 5.35 0.00 377.2 377.0
- 8* 1 8 2.8 12.9 25.5 2.02 0.04 4.65 0.04 302.4 299.6
- 9* 1 9 12.6 9.5 22.3 1.49 0.26 3.43 0.26 164.6 151.9
- 10* 1 10 7.9 7.2 17.4 0.88 0.28 2.58 0.28 73.3 65.5
- 11* 1 11 9.8 5.2 13.3 0.52 0.59 1.87 0.59 31.2 21.4
- 12* 1 12 30.3 3.7 10.1 0.29 3.19 1.34 1.34 12.7 -17.6
-
- [WIN] 14:10:11.33 G:\MIKE\SAMSUL\ANALYSIS >
-
-
- SCREEN OUTPUT OF SUBSEQUENT RUNS:
-
- [WIN] 14:15:06.72 G:\MIKE\SAMSUL\ANALYSIS >sim x.x
- Using input file x.x
- Writing output file x
- 1*
- 2*
- 3*
- 4*
- 5*
- 6*
- 7*
- 8*
- 9*
- 10*
- 11*
- 12*
-
- [WIN] 14:15:10.13 G:\MIKE\SAMSUL\ANALYSIS >
- [or sometimes it just crashes, GPFs, or sez "abnormal program execution"]
- */
-